package com.yd.app.mapper.provider;

import com.yd.domain.sys.Logger;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-12 14:17
 * @desc 日志provider
 **/
public class LoggerProvider extends BaseProvider{
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/12
     * @Param
     */
    public String searchLoggerList(Map map){
        Logger searchParam= (Logger) map.get("searchParam");
        StringBuilder sb=new StringBuilder(" select log.*,user.realName as operUserName from sys_logger log " +
                " left join sys_user user on log.operUser=user.id " +
                " where 1=1 ");
        sb.append(createSearchSanWhereSql(searchParam));
        sb.append(" limit #{startRow},#{pageSize} ");
        return sb.toString();
    }

    /**
     * 分页搜索总条数
     * @param map
     * @return
     */
    public String searchLoggerCount(Map map){
        Logger searchParam= (Logger) map.get("searchParam");
        StringBuilder sb=new StringBuilder(" select count(1) from sys_logger log " +
                " left join sys_user user on log.operUser=user.id " +
                " where 1=1 ");
        sb.append(createSearchSanWhereSql(searchParam));
        return sb.toString();
    }

    /**
     * 日志查询条件
     * @param  searchParam
     * @return
     */
    private String createSearchSanWhereSql(Logger searchParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(searchParam.getTitle())){
            sb.append(" and log.title like '%"+searchParam.getTitle()+"%' ");
        }
        if(!StringUtils.isEmpty(searchParam.getStatus())){
            sb.append(" and log.status=#{searchParam.status}  ");
        }
        if(!StringUtils.isEmpty(searchParam.getOperUser())){
            sb.append(" and log.operUser=#{searchParam.operUser}  ");
        }
        if(!StringUtils.isEmpty(searchParam.getOperUserName())){
            sb.append(" and user.realName like '%"+searchParam.getOperUserName()+"%' ");
        }
        if(!StringUtils.isEmpty(searchParam.getLog_type())){
            sb.append(" and log.log_type=#{searchParam.log_type}  ");
        }
        if(!StringUtils.isEmpty(searchParam.getIpAddress())){
            sb.append(" and log.ipAddress=#{searchParam.ipAddress}  ");
        }
        if(!StringUtils.isEmpty(searchParam.getModelType())){
            sb.append(" and log.modelType=#{searchParam.modelType}  ");
        }
        return sb.toString();
    }


}
